//
//  AddressBookViewController.m
//  IMDome
//
//  Created by limaohuyu on 2020/8/13.
//  Copyright © 2020 czw. All rights reserved.
//

#import "AddressBookViewController.h"
#import "BaseServiceManager.h"
#import "WebSocketRocketManger.h"
#import "ChatRoomViewController.h"
#import "AddFriendViewController.h"

#import "AddressBookTableViewCell.h"

#import "FriendModel.h"

@interface AddressBookViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSMutableArray  * dataArray ;
@end

@implementation AddressBookViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的朋友";
    [self.tableView registerNib:@"AddressBookTableViewCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UIButton *addBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    [addBtn setTitle:@"添加" forState:UIControlStateNormal];
    [addBtn setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];
    addBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [addBtn addTarget:self action:@selector(addBtnAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *btnItem = [[UIBarButtonItem alloc]initWithCustomView:addBtn];
    self.navigationItem.rightBarButtonItem  =btnItem;
 
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self laodData];

}
- (void)laodData{
     NSMutableDictionary *params  = [NSMutableDictionary dictionary];
    [params setObject:[APPUserDataManager shareInstance].userId forKey:@"userId"];
     [BaseServiceManager getRequest:@"friend/getAllFriends" params:params startBlock:^{
         [self showRequestHUDView];
      } successBlock:^(ResponseBaseModel * _Nonnull responseModel) {
          [self hideRequestHUDView];
          self.dataArray =  [FriendModel mj_objectArrayWithKeyValuesArray:responseModel.data];
          [self.tableView reloadData];
          
      } failureBlock:^(NSError * _Nonnull error, NSString * _Nonnull msg) {
          [self hideRequestHUDView];
      }];
}

#pragma mark == UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    
    return self.dataArray.count;
    
}


- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    AddressBookTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddressBookTableViewCell" forIndexPath:indexPath];
    FriendModel *model =  self.dataArray[indexPath.row];
    cell.data = model;
    return cell;    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    FriendModel *model =  self.dataArray[indexPath.row];
    ChatRoomViewController *customerServicesVC = [ChatRoomViewController new];
    customerServicesVC.receiveId = model.friendId;
    [self.navigationController pushViewController:customerServicesVC animated:YES];
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}


- (void)addBtnAction{
    AddFriendViewController *addFriendVC = [AddFriendViewController new];
    [self.navigationController pushViewController:addFriendVC animated:YES];
    
}
@end
